package com.htt.base_library.network

import com.htt.base_library.network.https.SSLSocketClient
import com.htt.base_library.util.LogUtils
import com.test.library_base.BuildConfig
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.TimeUnit

@Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
abstract class BaseNetwork {
    private var baseUrl: String? = ""
    private var okHttpClient: OkHttpClient? = null

    constructor(url: String?) {
        baseUrl = url
    }


    companion object {
        var DEFAULT_TIME_OUT = 20000L

        var retrofitMap = ConcurrentHashMap<String, Retrofit>()
    }


    private fun getRetrofit(): Retrofit? {
        return baseUrl?.let {
            var retrofit = retrofitMap[it]
            if (retrofit == null) {
                val retrofitBuilder = Retrofit.Builder()
                retrofitBuilder.baseUrl(it)
                retrofitBuilder.client(createOkHttpClient())
                retrofitBuilder.addConverterFactory(GsonConverterFactory.create())
                //retrofitBuilder.addCallAdapterFactory(LiveDataCallAdapterFactory())
                retrofit = retrofitBuilder.build()
                retrofitMap[it] = retrofit
            }
            retrofit
        }
    }

    private fun createOkHttpClient(): OkHttpClient {
        if (okHttpClient == null) {
            val okHttpClientBuilder = OkHttpClient.Builder()

            val cacheSize = 100 * 1024 * 1024 // 10MB
//            val cacheDir=if(SDCardUtils.isSDCardEnableByEnvironment()) Utils.getApp().externalCacheDir else Utils.getApp().cacheDir
//
//            cacheDir?.also {
//                okHttpClientBuilder.cache(
//                    Cache(
//                        it, cacheSize.toLong())
//                )
//            }
            //配置超时时间
            okHttpClientBuilder.connectTimeout(DEFAULT_TIME_OUT, TimeUnit.SECONDS)
            okHttpClientBuilder.readTimeout(DEFAULT_TIME_OUT, TimeUnit.SECONDS)
            okHttpClientBuilder.writeTimeout(DEFAULT_TIME_OUT, TimeUnit.SECONDS)
            okHttpClientBuilder.retryOnConnectionFailure(true)
            //配置忽略证书验证
//            okHttpClientBuilder.sslSocketFactory(SSLSocketClient.getSSLSocketFactory(),SSLSocketClient.getX509TrustManager())//配置
//            okHttpClientBuilder.hostnameVerifier(SSLSocketClient.getHostnameVerifier())//配置
            configOkHttp(okHttpClientBuilder)
            if (BuildConfig.DEBUG) {
                val httpLoggingInterceptor = HttpLoggingInterceptor()
                httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
                okHttpClientBuilder.addInterceptor(httpLoggingInterceptor)
            }else{
                okHttpClientBuilder.addInterceptor(DomainSwitchInterceptor())
            }
            okHttpClient = okHttpClientBuilder.build()
        }
        return okHttpClient as OkHttpClient
    }

    fun <T> create(service: Class<T>?): T =
        getRetrofit()?.create(service!!) ?: throw RuntimeException("Api service is null!")

    /**
     * 配置Okhttp拦截器
     */
    abstract fun configOkHttp(builder: OkHttpClient.Builder?)
}